#pragma once

#ifdef USE_LIBUV
#include "../core/Common.h"

struct uv_tcp_s;
struct uv_stream_s;

namespace LuaGui {
	class TcpSocket {
	public:
		TcpSocket();
		~TcpSocket();

		static TcpSocket* GetTcpSocketByUID(uint64_t uid);
		static TcpSocket* Connect(std::string ip = "0.0.0.0", unsigned int port = 80);

		int Send(std::string buf);
		void OnRecv();
		void Close();
		unsigned long long GetUID() { return (unsigned long long)this; }
		unsigned long long GetServerUID() { return m_server_uid; }
		void SetServerUID(unsigned long long uid) { m_server_uid = uid; }
		std::string GetIp();
		unsigned int GetPort();
		std::string GetPeerIp();
		unsigned int GetPeerPort();

	public:
		struct uv_stream_s* GetUVStream() { return (struct uv_stream_s*)m_uv_tcp; }
		struct uv_tcp_s* GetUVTcp() { return m_uv_tcp; }
	private:
		struct uv_tcp_s* m_uv_tcp;
		unsigned long long m_server_uid;
		static std::unordered_map<uint64_t, TcpSocket*> s_tcp_sockets;
	};
}

#endif